package db.entity;

import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;

import db.BaseDAO;
import db.DAOFactory;
import db.Transaction;
import db.TransformInterface;

import model.Node;
import model.Spot;

/**
 * Description: 对景点数据进行处理的DAO。
 * 
 * @since  2009/2/15
 * @author ashen
 * 
 */
public class SpotDAO extends BaseDAO implements TransformInterface<Spot>{
	
	public SpotDAO(){
		setTableName("mapscheme.spot");
	}
	
	/**
	 * the method save of class SpotDAO
	 * 
	 * 该方法将参数景点对象存储到数据库
	 * 
	 * @param spot 需要保存的Spot对象
	 * @return 返回操作成功与否
	 */
	public boolean save(Spot spot){
		Node node=spot.toNode();
		if(!DAOFactory.getNodeDAO("spot").save(node))
			return false;
		Transaction transaction=new Transaction();
		node=DAOFactory.getNodeDAO().findByName(spot.getName(),transaction);

		int id=node.getId();
		spot.setId(id);
		save(object2HashMap(spot),transaction);
		if(transaction.commit())
			return true;
		else{
			DAOFactory.getNodeDAO().delete(id);
			return false;
		}			
	}
	
	/**
	 * the method update of class SpotDAO 
	 * 
	 * 该方法将参数景点对象更新到数据库
	 * 
	 * @param spot 需要更新的Point对象
	 * @return 返回操作成功与否
	 */
	public boolean update(Spot spot){
		Transaction transaction=new Transaction();
		Node node=spot.toNode();
		DAOFactory.getNodeDAO("spot").update(node,node.getId(),transaction);
		super.update(object2HashMap(spot),"ID",transaction);
		return transaction.commit();
	}
	
	/**
	 * the method delete of class SpotDAO
	 * 
	 * 该方法删除指定ID的景点
	 * 
	 * @param id 需要删除的景点的ID
	 * @return 返回操作成功与否
	 */
	public boolean delete(int id){
		return DAOFactory.getNodeDAO().delete("id",id);
	}

	/**
	 * the method findAll of class SpotDAO
	 * 
	 * 该方法返回所有景点
	 * 
	 * @return ArrayList<Spot>类型，返回数据中所有景点
	 */
	public ArrayList<Spot> findAll(){
		Spot spot=null;
		Node node=null;
		Transaction transaction=new Transaction();
		ArrayList<Spot> resultList=new ArrayList<Spot>();
		ArrayList<Node> nodeList=DAOFactory.getNodeDAO().findByType("spot", transaction);
		for(Iterator<Node> i=nodeList.iterator();i.hasNext();){
			node=(Node)i.next();
			spot=this.findById(node.getId(),transaction);
			spot.setSpot(node);
			resultList.add(spot);
		}
		transaction.close();
		return resultList;
	}
	
	/**
	 * the method findById of class SpotDAO
	 * 
	 * 该方法通过指定景点ID查找景点
	 * 
	 * @param id 景点ID
	 * @param transaction 数据库操作类Transaction的对象
	 * @return 查询到的景点
	 */
	public Spot findById(Integer id,Transaction transaction){	        		 	        
        Spot spot= hashMap2Object(findByPK("id",id,transaction));
        if(spot!=null)
        	spot.setSpot(DAOFactory.getNodeDAO().findById(id,transaction));
        return spot;
	}
	
	/**
	 * the method findById of class SpotDAO
	 * 
	 * 该方法通过指定景点ID查找景点
	 * 
	 * @param id 景点ID
	 * @return 查询到的景点
	 */
	public Spot findById(Integer id){
		Transaction transaction=new Transaction();
        Spot spot= this.findById(id, transaction);
        transaction.close();
        return spot;
	}
	
	/**
	 * the method findByName of class SpotDAO
	 * 
	 * 该方法通过指定景点名称查找景点
	 * 
	 * @param name 景点名称
	 * @return 查询到的景点列表
	 */
	public Spot findByName(String name){	
		Transaction transaction=new Transaction();
		Node node=DAOFactory.getNodeDAO().findByName(name, transaction);
		Spot spot=this.findById(node.getId(),transaction);
		spot.setSpot(node);
		transaction.close();
		return spot;
	}
	
	/**
	 * the method findByKeyword of class SpotDAO
	 * 
	 * 该方法通过指定景点关键字查找景点
	 * 
	 * @param name 景点名称
	 * @param description 景点描述
	 * @return 查询到的景点列表
	 */
	public ArrayList<Spot> findByKeyword(String name,String description){	        		
		Spot spot=null;
		Node node=null;
		Transaction transaction=new Transaction();
		ArrayList<Spot> resultList=new ArrayList<Spot>();
		ArrayList<Node> nodeList=
			DAOFactory.getNodeDAO().findByKeyword(name,description,"spot",transaction);
		for(Iterator<Node> i=nodeList.iterator();i.hasNext();){
			node=(Node)i.next();
			spot=this.findById(node.getId(),transaction);
			spot.setSpot(node);
			resultList.add(spot);
		}
		transaction.close();
		return resultList;
	}
	
	/**
	 * the method findBySightId of class SpotDAO
	 * 
	 * 该方法找到id为参数值的风景sight的所有景点，并返回
	 * 
	 * @param sightId 风景所在景点id
	 * @return 景点记录集
	 */
	public ArrayList<Spot> findBySightId(int sightId){ 
		Spot spot=null;
		Node node=null;
		HashMap tempHashMap=null;
		ArrayList<Spot> resultList=new ArrayList<Spot>();
		Transaction transaction=new Transaction();
		ArrayList<HashMap> list=super.findByProperty("sightId", sightId, transaction);		
		for(Iterator<HashMap> i=list.iterator();i.hasNext();){
			tempHashMap=(HashMap)i.next();
			spot=this.hashMap2Object(tempHashMap);
			Integer id=(Integer)tempHashMap.get("ID");
			node=DAOFactory.getNodeDAO().findById(id,transaction);
			spot.setSpot(node);
			resultList.add(spot);
		}
		transaction.close();
		return resultList;
	}
	
	/**
	 * the method list2ObjectList of class SpotDAO
	 * 
	 * 该方法将存有景点信息的HashMap对象数组转换为Point对象数组
	 * 
	 * @param hashMapList HashMap对象数组
	 * @return Point对象数组
	 */
	public ArrayList<Spot> list2ObjectList(ArrayList<HashMap> hashMapList){
		Spot tempPoint=null;
		ArrayList<Spot> resultList=new ArrayList<Spot>();
		if(hashMapList.isEmpty())
			return resultList;
		for(Iterator i=hashMapList.iterator();i.hasNext();){
			tempPoint=hashMap2Object((HashMap)i.next());
			if(tempPoint!=null)
				resultList.add(tempPoint);
		}
		return resultList;
	}
	
	/**
	 * the method hashMap2Object of class SpotDAO
	 * 
	 * 该方法将HashMap类型的对象转换为Spot的对象，并返回Spot的对象.
	 * 
	 * @param hashMap 存储了一个景点信息的HashMap类型的对象
	 * @return Spot的对象.
	 */
	public Spot hashMap2Object(HashMap hashMap){
		if(hashMap==null)
			return null;
		Spot spot=new Spot();
		int id;
		Spot tempSight;
		spot.setMapId((Long)hashMap.get("MAPID"));		
		spot.setXpos((Integer)hashMap.get("XPOS"));
		spot.setYpos((Integer)hashMap.get("YPOS"));
		if(hashMap.get("SIGHTID")!=null){
			id=(Integer)hashMap.get("SIGHTID");
			tempSight=findById(id);
			spot.setSight(tempSight);
		}
		spot.setPrice((Integer)hashMap.get("PRICE"));
		spot.setUrl((String)hashMap.get("URL"));
		return spot;
	}
	
	/**
	 * the method object2HashMap of class SpotDAO
	 * 
	 * 该方法将参数Spot对象转换为存储参数Spot对象信息的HashMap对象，并返回
	 * 
	 * @param spot 景点Spot对象
	 * @return 存储参数景点Spot对象信息的HashMap对象
	 */
	public HashMap object2HashMap(Spot spot){
		HashMap hashMap=new HashMap();
		hashMap.put("ID", spot.getId());
		hashMap.put("MAPID", spot.getMapId());
		hashMap.put("XPOS", spot.getXpos());
		hashMap.put("YPOS", spot.getYpos());
		hashMap.put("SIGHTID", spot.getSight().getId());
		hashMap.put("PRICE", spot.getPrice());
		hashMap.put("URL", getFormatString(spot.getUrl()));
		return hashMap;
	}

}